#!/usr/bin/env python
# -*- coding:utf-8 -*-

# import itertools
from itertools import *

def index_words(text):
    result = []
    if text:
        result.append(0)
    for index, letter in enumerate(text):
        if letter == ' ':
            result.append(index +1)
    return result


def index_words_iter(text):
    """
    最后返回的是迭代器,结果需要传给list()转换
    """
    if text:
        yield 0
    for index, letter in enumerate(text):
        if letter == ' ':
            yield index + 1


def index_file(handle):
    offset = 0
    for line in handle:
        if line:
            yield offset
        for letter in line:
            offset += 1
            if letter == ' ':
                yield offset


address = 'Four score and seven years ago...'

result = index_words(address)
print(result[:3])

result = list(index_words_iter(address))
print(result[:3])

with open('./16_gemerator_replace_list.py_address.txt', 'r') as f:
    it = index_file(f)
    result = islice(it, 0, 3)
    print(list(result))
